
#include "core.h"
#include "../util/util.h"

#include <v8.h>

using namespace v8;

namespace js {
namespace bindings {
namespace core {

static int g_dump_counter = 0;

static Handle<Value> dump(const Arguments& args) {
    if (args.Length() > 0) {
        Handle<Value> str_value(args[0]);
        String::AsciiValue str(str_value);
        FILE * os = stdout;

        fprintf(os, "#%04d: ", ++g_dump_counter);
        fputs(*str, os);
        fputc('\n', os);

        fflush(os);

        return str_value;
    }

    return Undefined();
}

static Handle<Value> include(const Arguments& args) {
    if (args.Length() > 0) {
        String::AsciiValue str(args[0]);
        return Int32::New(js::run_js_script(*str));
    }
    return Undefined();
}

// see http://code.google.com/apis/v8/embed.html for more info
void add_globals(Handle<ObjectTemplate>& globals) {
    globals->Set("include", FunctionTemplate::New(&include));
    globals->Set("dump", FunctionTemplate::New(&dump));
}

} // namespace core
} // namespace bindings
} // namespace js



